<template>
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="标段号" prop="markCode">
        <el-input
          clearable
          :maxlength="50"
          v-model="queryParams.markCode"
          placeholder="请输入标段号"
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select clearable v-model="queryParams.status" placeholder="请选择" class="!w-200px">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.BID_SECTION_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审批状态" prop="approvalState">
        <el-select
          clearable
          v-model="queryParams.approvalState"
          placeholder="请选择"
          class="!w-200px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.BID_APPROVAL_STATUS_SHOW)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="竞拍开始时间" prop="tradingTime">
        <el-date-picker
          v-model="queryParams.tradingTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          type="datetimerange"
          class="!w-400px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['bid:section:export']"
        >
          <Icon icon="ep:download" class="mr-5px" />
          导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <ContentWrap>
    <!-- 列表 -->
    <el-table
      v-loading="loading"
      :data="list"
      border
      stripe
      :header-cell-style="{ background: '#F3F4F7', color: '#555' }"
    >
      <el-table-column label="编号" align="center" width="60" fixed="left">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="标段号"
        align="center"
        prop="markCode"
        min-width="200"
        fixed="left"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="场次编号" align="center" prop="sessionCode" min-width="150" />
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template #default="{ row }">
          <dict-tag :type="DICT_TYPE.BID_SECTION_STATUS" :value="row.status" />
        </template>
      </el-table-column>
      <el-table-column label="审批状态" align="center" prop="approvalState" width="100">
        <template #default="{ row }">
          <dict-tag
            v-if="row.approvalState"
            :type="DICT_TYPE.BID_APPROVAL_STATUS_SHOW"
            :value="row.approvalState"
          />
          <span v-else></span>
        </template>
      </el-table-column>
      <el-table-column label="起拍价格(元/吨)" align="center" prop="startingPrice" width="150" />
      <el-table-column label="中标价格(元/吨)" align="center" prop="bidPrice" width="150" />
      <el-table-column label="中标量(吨)" align="center" prop="bidAmount" width="100" />
      <el-table-column
        label="中标客户"
        align="center"
        prop="customerName"
        min-width="200"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="竞拍时间(开始)"
        align="center"
        prop="startTime"
        width="180"
        :formatter="dateFormatter"
      />
      <el-table-column
        label="竞拍时间(结束)"
        align="center"
        prop="endTime"
        width="180"
        :formatter="dateFormatter"
      />
      <el-table-column label="产品名称" align="center" prop="coalName" width="150">
      </el-table-column>
      <el-table-column
        label="交易量(吨)"
        align="center"
        prop="tradingAmount"
        width="150"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="交易方式"
        align="center"
        prop="tradingWay"
        width="150"
        :show-overflow-tooltip="true"
      >
        <template #default="{ row }">
          {{ getDictLabel(DICT_TYPE.BID_TRADING_WAY, row.tradingWay?.toString()) }}
        </template>
      </el-table-column>
      <el-table-column
        label="发热量"
        align="center"
        prop="calorificValue"
        width="100"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="灰度"
        align="center"
        prop="ashContent"
        width="100"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="硫份"
        align="center"
        prop="sulfurContent"
        width="100"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="水份"
        align="center"
        prop="hydration"
        width="100"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        min-width="200"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="操作" align="center" min-width="150" fixed="right">
        <template #default="{ row }">
          <el-button
            v-if="row.approvalState === 10"
            link
            type="primary"
            v-hasPermi="['bid:section:approve']"
            @click="approvalFn(row.id)"
            >审批</el-button
          >
          <el-button
            v-if="row.status === 5"
            link
            type="primary"
            v-hasPermi="['bid:section:copy']"
            @click="listingFn(row)"
            >挂牌</el-button
          >
          <el-button
            link
            type="primary"
            v-hasPermi="['bid:bidRecord:list']"
            @click="linkCompetitionRecords(row)"
            >竞价记录</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <Approval ref="approval" @success="success"></Approval>
</template>
<script setup lang="ts" name="mySubject">
import * as Utils from '@/utils/dict'
import download from '@/utils/download'
import * as subjectManage from '@/api/subjectManage'
import { dateFormatter } from '@/utils/formatTime'
import Approval from './approval.vue'
import { useCommonStoreOut } from '@/store/modules/common'

const { DICT_TYPE, getIntDictOptions, getDictLabel } = reactive(Utils)
const loading = ref(true) // 列表的加载中
const exportLoading = ref(false) // 列表的加载中
const message = useMessage()
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const useCommonStore = useCommonStoreOut()
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  markCode: undefined,
  status: undefined,
  approvalState: undefined,
  tradingTime: []
})
const queryFormRef = ref()
const approval = ref()
const router = useRouter()

/** 查询列表 */

const getList = async () => {
  loading.value = true
  try {
    const data = await subjectManage.getPlatformPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const success = () => {
  handleQuery()
}

const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await subjectManage.exportPlatformExcel(queryParams)
    download.excel(data, '标段管理列表.xls')
  } finally {
    exportLoading.value = false
  }
}

const approvalFn = (id) => {
  approval.value.open(id)
}

const linkCompetitionRecords = (row) => {
  router.push({
    path: '/bidManageAdmin/bidding/competitionRecords',
    query: { markCode: row.markCode }
  })
}

const listingFn = (row) => {
  useCommonStore.setFormData({ index: 1, sessionId: row.sessionId })
  router.push({ path: '/bidManageAdmin/announceManage' })
}

/** 初始化 **/
onMounted(() => {
  getList()
})

defineExpose({})
</script>
